package com.leetcode.basic.synchronize.ReentrantLock;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.locks.ReentrantLock;

public class LockInterrupt extends Thread {

	public static ReentrantLock lock1 = new ReentrantLock();
	public static ReentrantLock lock2 = new ReentrantLock();

	int id;

	public LockInterrupt(int id, String name) {
		super(name);
		this.id = id;
	}

	@Override
	public void run() {
		try {
			if (id == 1) {
				lock1.lockInterruptibly();
				try {
					Thread.sleep(500);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("[" + id + "] lock1.lockInterruptibly();");
				lock2.lockInterruptibly();
			} else {
				lock2.lockInterruptibly();
				try {
					Thread.sleep(500);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("[" + id + "] lock1.lockInterruptibly();");
				lock1.lockInterruptibly();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (lock1.isHeldByCurrentThread()) {
				lock1.unlock();
			}
			if (lock2.isHeldByCurrentThread()) {
				lock2.unlock();
			}
			System.out.println(Thread.currentThread().getId() + ":线程退出");
		}
	}

	public static void main(String[] args) throws InterruptedException {
		// 先做一对死锁
		LockInterrupt t1 = new LockInterrupt(1, "LockInterrupt1");
		LockInterrupt t2 = new LockInterrupt(2, "LockInterrupt2");
		t1.start();
		t2.start();

		Thread.sleep(1000);

		// 打断 ReentrantLock
		DeadlockChecker.check();
	}

	static class DeadlockChecker {

		private final static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();

		public static void check() {
			Thread tt = new Thread(() -> {
				while (true) {
					long[] deadlockedThreadIds = mbean.findDeadlockedThreads();
					if (deadlockedThreadIds != null) {
						ThreadInfo[] threadInfos = mbean.getThreadInfo(deadlockedThreadIds);
						for (Thread t : Thread.getAllStackTraces().keySet()) {
							for (int i = 0; i < threadInfos.length; i++) {
								if (t.getId() == threadInfos[i].getThreadId()) {
									System.out.println(t.getName());
									t.interrupt();
								}
							}
						}
					}
					try {
						Thread.sleep(5000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			tt.setDaemon(true);
			tt.start();
		}

	}

}